package com.base.game;

import com.base.engine.MADEngine;
import com.base.engine.game.MADGame;
import com.base.engine.gui.MADUserInterface;
import com.base.engine.system.EDirection;
import com.base.game.entity.Camera;
import com.base.game.physics.Physics;
import com.base.game.terrain.Terrain;
import static org.lwjgl.opengl.GL11.*; 

public class Game extends MADGame {

	private Terrain terrain ;
	private Physics physics ; 
	private Camera player ; 
		
	public Game(String title) {
		super(title);
	}

	@Override
	public void init() {
		this.terrain = new Terrain(); 
		this.physics = new Physics(this.terrain); 
		this.player = new Camera(this.physics, Terrain.getCenter());
		MADUserInterface.getDebuggingScreen().getInfo().add("Position absolute: ... ");  // 3 
		MADUserInterface.getDebuggingScreen().getInfo().add("Chunk position: ... ");   // 4 
		MADUserInterface.getDebuggingScreen().getInfo().add("Northern chunk limit: ... ");    // 5 
		MADUserInterface.getDebuggingScreen().getInfo().add("Southern chunk limit: ... ");    // 6 
		MADUserInterface.getDebuggingScreen().getInfo().add("Western chunk limit: ... ");    // 7 
		MADUserInterface.getDebuggingScreen().getInfo().add("Eastern chunk limit: ... ");    // 8 
		MADUserInterface.getDebuggingScreen().getInfo().add("Moving: ... ") ; 	// 9
		MADUserInterface.getDebuggingScreen().getInfo().add("Position in active chunk: ... "); 	// 10 
		this.setRunning(true); 			
	}

	@Override
	public void getInput() {
		InputHandler.getKeyboardInput(this); 
		InputHandler.getMouseInput(this); 
	}

	@Override
	public void update() {
		this.terrain.update(player.getPos()); 
		this.player.update(); 
		MADUserInterface.getDebuggingScreen().getInfo().set(3, player.toString()); 
		MADUserInterface.getDebuggingScreen().getInfo().set(4, "Chunk position: "+this.terrain.getCenterChunkID(this.player.getPos())); 
		MADUserInterface.getDebuggingScreen().getInfo().set(10, 
				"Position in active chunk: "+this.terrain.getBlockPositionInChunk(this.player.getPos())); 
	}

	@Override
	public void render() {
		glPushMatrix(); 
			glRotatef(this.player.getPitch(), 1, 0, 0); 
			glRotatef(this.player.getYaw(), 0, 1, 0); 
			glTranslatef(this.player.getPos().getX(), this.player.getPos().getY()-1, this.player.getPos().getZ()); 
			this.terrain.render(); 
		glPopMatrix(); 
	}

	public void moveTerrainLimit(EDirection dir){
		terrain.moveLimits(dir); 
	}
	
	public void move(EDirection dir) {
		this.player.move(dir); 
	}
	
	public void yaw(float val){
		this.player.yaw(val); 
	}
	public void pitch(float val){
		this.player.pitch(val); 
	}

	public static void main(String[] args) throws Exception{
		MADEngine.create(new Game("MADVoxels"), false); 
	}
	
}
